// pages/exam/exam.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['一年级', '二年级', '三年级'],
    array1:['语文','数学','英语'],
    array2:['小学','初中','高中'],
    gradeName:'',
    subject:'',
    groupName:'',
    tip:"请选择考试科目信息!"
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let params=this.data.array[e.detail.value];
    this.setData({
      index: e.detail.value,
      gradeName:params
    })
  },
  bindPickerChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    let params=this.data.array1[e.detail.value];
    this.setData({
      index1: e.detail.value,
      subject:params
    })
},
  bindPickerChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    let params=this.data.array2[e.detail.value];
    this.setData({
      index2: e.detail.value,
      groupName:params
    })
  },
  startexam:function(){
    if(this.data.gradeName.length==0||this.data.subject.length==0||this.data.groupName.length==0){
      wx.showModal({
        title: '操作提示',
        content: this.data.tip,
        showCancel: false,
        confirmColor: '#2db7f5',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
    wx.setStorage({
      data: '746122920027226112',
      key: 'accountId',
    }) 
    let accountId = wx.getStorageSync('accountId')
    let that = this
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/exam/findExamList',
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        "gradeName": this.data.gradeName,
        "groupName":  this.data.groupName,
        "subject": this.data.subject,
        "tenantId": accountId
      },
      success: function(res){
        let paperId = res.data.data[0].id
        // console.log(paperId)
        // 选择其他试卷时,清理缓存,还没写防范于未然
        if (wx.getStorageSync('paperId') != paperId){
          wx.clearStorageSync()
        }
        let index = 0
        // 定时器初始化
        let minute = 20
        let second = 0
        let time = new Array()
        time[0] = minute
        time[1] = second
        let indexCompare = wx.getStorageSync('index')
        if (indexCompare.length != 0) {
          wx.showModal({
            title: '提示',
            content: '检测到你上一次考试未完成，请问你是需要:',
            cancelText: '重新答题',
            confirmText: '继续答题',
            success (res) {
              if (res.confirm) {
                index = wx.getStorageSync('index')
                
              } else if (res.cancel) {
                wx.setStorage({
                  data: time,
                  key: 'time',
                })
                wx.setStorage({
                  data: index,
                  key: 'index',
                })
                wx.setStorage({
                  data: paperId,
                  key: 'paperId',
                })
                wx.removeStorageSync('checked')
              }
              that.choiceUrl(index)
            },
          })
        } else {
          wx.setStorage({
            data: time,
            key: 'time',
          })
          wx.setStorage({
            data: index,
            key: 'index',
          })
          wx.setStorage({
            data: paperId,
            key: 'paperId',
          })
          that.choiceUrl(index)
        }     
      }
    })
  }
 },

  // 跳转试卷
  choiceUrl: function(index) {
    let paperId = wx.getStorageSync('paperId')
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/exam/paper-stem/' + String(paperId), //仅为示例，并非真实的接口地址
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        let data = JSON.stringify(res.data.data)
        let dataAll = res.data

        wx.setStorage({
          data: data,
          key: 'data',
        })
        if (wx.getStorageSync('checked') == ''){
          var arr = new Array(10).fill(0)
          wx.setStorage({
            data: arr,
            key: 'checked',
          })
        }  
        if (dataAll.data[index].type == "SINGLE_CHOICE") {
          wx.redirectTo({
            url: '/pages/choose/choose',
          })
        } else if (dataAll.data[index].type == "MULTIPE_CHOICE") {
          wx.redirectTo({
            url: '/pages/Multiple/Multiple',
          })
        } else if (dataAll.data[index].type == "JUDGE") {
          wx.redirectTo({
            url: '/pages/judge/judge',
          })
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})